def count_words(path, filename):
    """计算一个文件大致包含多少个单词"""
    try:
        with open(path+filename) as f_obj:
            contents = f_obj.read()
    except FileNotFoundError:
        msg = "Sorry, the file " + filename + " does not exist."
        print(msg)
    else:
        # 计算文件大致包含多少个单词
        words = contents.split()  # split()根据字符串创建单词列表
        aa = contents.count('love')  # count()确定特定单词在字符串中出现的次数
        bb = words.count('love')  # count()确定特定单词在列表中出现的次数
        num_words = len(words)
        print("The file " + filename + " has about " + str(num_words) + " words.")
        print(aa)
        print(bb)


path = '/Users/xjf/Documents/Python_work/text_files/'
filename = 'programming.txt'

count_words(path, filename)